package com.lianniu.mgr.saleManage.controller;

import javax.servlet.http.HttpServletRequest;

import com.lianniu.mgr.saleManage.model.SkuProductLogConditionVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.service.IAdminAreaService;
import com.lianniu.mgr.buyerManage.service.IBuyerManageService;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductBrandVo;
import com.lianniu.mgr.saleManage.model.ProductShopVo;
import com.lianniu.mgr.saleManage.model.SkuProductConditionVo;
import com.lianniu.mgr.saleManage.model.SkuProductVo;
import com.lianniu.mgr.saleManage.service.IProductShopService;
import com.lianniu.mgr.saleManage.service.ISkuProductService;
import com.lianniu.mgr.supplyManage.dao.ISupplyManageDao;
import com.lianniu.mgr.supplyManage.service.ISupplyManageService;
@Controller
@RequestMapping("/rest/saleManager")
public class SkuProductRestController {
	
	private static final Logger logger = Logger.getLogger(SkuProductRestController.class);
	@Autowired
	ISkuProductService skuProductService;
	@Autowired
	IAdminAreaService adminAreaService;
	@Autowired
	ISupplyManageService supplyManageService;
	@Autowired
	IProductShopService productShopService;
	@Autowired
	IBuyerManageService buyerManageService;
	/**
	 * 
	 * @Title: skuProductList 
	 * @Description: 获取发布商品列表
	 * @author: guoconglin
	 * @param request
	 * @param paginationInfo
	 * @param condition
	 * @return
	 * @date: 2016年12月8日 上午11:38:17 
	 * @return: BaseResult
	 */
	@RequestMapping(value="/skuProductList",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult skuProductList(HttpServletRequest request,PaginationInfo paginationInfo,SkuProductConditionVo condition){
		 logger.info("SkuProductRestController  skuProductList begin.");
	       try {
	           logger.info("skuProductList begin."); 
	           String productName = request.getParameter("productName");
	           String buyerAreaName = request.getParameter("buyerAreaName");
	           String supplyName = request.getParameter("supplyName");
               String areaId = request.getParameter("countyId");
               BaseResult baseResult = null ;
               if("" !=productName || "" != buyerAreaName || "" != supplyName || "" != areaId ){
                   condition.setProductName(productName);
                   condition.setBuyerAreaName(buyerAreaName);
                   condition.setSupplyName(supplyName);
                   condition.setAreaId(areaId);
               }
	           
	           baseResult = skuProductService.selectBySkuProductForPage(condition, paginationInfo);
	           logger.info("skuProductList end.");
	           logger.info("SkuProductRestController skuProductList end.");
	           logger.info("basesResult:"+JSON.toJSONString(baseResult));
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("SkuProductRestController skuProductList error:" + e.getMessage());
	       logger.error("SkuProductRestController skuProductList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	}
	    /**
	     * @Title: changeStatusSwitchs 
	     * @Description: 下架
	     * @author: guoconglin
	     * @param request
	     * @return
	     * @date: 2016年8月9日 下午8:11:59 
	     * @return: BaseResult
	     */
	  @RequestMapping(value = {"/changeStatusSwitchs"}, method = RequestMethod.POST)
      public @ResponseBody BaseResult changeStatusSwitchs(HttpServletRequest request) {
          logger.info("SkuProductRestController changeStatusSwitchs begin.");      
          try {
              logger.info("changeStatusSwitchs begin.");
              BaseResult baseResult = skuProductService.updateById(request.getParameter("ids").split(","));
              logger.info("changeStatusSwitchs baseResult:" + JSON.toJSONString(baseResult));
              logger.info("changeStatusSwitchs end.");
              return baseResult;

        } catch (Exception e) {
          logger.error("changeStatusSwitchs error:"+e.getMessage());
          logger.info("SkuProductRestController changeStatusSwitchs end.");
          return BaseResult.exception(e.getMessage());
        }
      }
	  /**
	   * @Title: changeStatusSwitch 
	   * @Description: 上架
	   * @author: guoconglin
	   * @param request
	   * @return
	   * @date: 2016年8月11日 上午10:39:47 
	   * @return: BaseResult
	   */
	  @RequestMapping(value = {"/changeStatusSwitch"}, method = RequestMethod.POST)
      public @ResponseBody BaseResult changeStatusSwitch(HttpServletRequest request) {
          logger.info("SkuProductRestController changeStatusSwitch begin.");      
          try {
              logger.info("changeStatusSwitch begin.");
              BaseResult baseResult = skuProductService.updateStatus(request.getParameter("ids"));
              logger.info("changeStatusSwitch baseResult:" + JSON.toJSONString(baseResult));
              logger.info("changeStatusSwitch end.");
              return baseResult;

        } catch (Exception e) {
          logger.error("changeStatusSwitch error:"+e.getMessage());
          logger.info("SkuProductRestController changeStatusSwitch end.");
          return BaseResult.exception(e.getMessage());
        }
      }
	  /**
	   * @Title: skuProductCancel 
	   * @Description: 取消发布
	   * @author: guoconglin
	   * @param request
	   * @return
	   * @date: 2016年8月9日 下午8:18:05 
	   * @return: BaseResult
	   */
	  @RequestMapping(value = {"/skuProductCancel"}, method = RequestMethod.POST)
      public @ResponseBody BaseResult skuProductCancel(HttpServletRequest request) {
          logger.info("SkuProductRestController skuProductCancel begin.");
         try {          
              logger.info("skuProductCancel begin.");
              BaseResult baseResult = skuProductService.deleteSkuProductBatch(request.getParameter("ids").split(","));      
              logger.info("skuProductCancel baseResult:" + JSON.toJSONString(baseResult));
              logger.info("skuProductCancel end.");
              
              logger.info("SkuProductRestController skuProductCancel end.");
              return baseResult;

        } catch (Exception e) {
          logger.error("SkuProductRestController skuProductCancel error:" + e.getMessage());
          logger.info("SkuProductRestController skuProductCancel end.");
          return BaseResult.exception(e.getMessage());
        }

      }
	  @RequestMapping(value = {"/getAreaName"}, method = {RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult getAreaName(String areaId){
	      BaseResult baseResult = skuProductService.getAreaName(areaId);
	      return BaseResult.success(baseResult);
	  }
	  /**
	   * 
	   * @Title: insertSkuProduct 
	   * @Description: 后台发布商品
	   * @author: alex
	   * @param request
	   * @param skuProductVo
	   * @return
	   * @date: 2016年12月8日 上午11:38:54 
	   * @return: BaseResult
	   */
	  @RequestMapping(value = {"/insertSkuProduct"}, method = {RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult insertSkuProduct(HttpServletRequest request,SkuProductVo skuProductVo,String mobile,String countyId){
	      BaseResult bs = null;
	      try {
	             ProductShopVo productShopVo = new ProductShopVo();
	             String productId = skuProductVo.getProductName();
	             productShopVo.setProductId(productId);
	             productShopService.insertProductShop(productShopVo);
	             String id = productShopVo.getId();
	             skuProductVo.setShopProductId(id);
	             //skuProductVo.setSaleType("3");
	             skuProductVo.setStockNum(skuProductVo.getOverPlusNum());
	             skuProductVo.setUnit("台");
			     skuProductVo.setSaleStatus("3");
	             skuProductVo.setApproveStatus("3");
	             skuProductVo.setIsPayOnline("1");
	             bs = skuProductService.insertSkuProduct(skuProductVo,mobile,countyId);
        } catch (Exception e) {
            logger.error("SkuProductRestController insertSkuProduct error:" + e.getMessage());
            logger.info("SkuProductRestController insertSkuProduct end.");
            return BaseResult.exception(e.getMessage());
        }
          return BaseResult.success(bs);
	  }
	  /**
	   * 
	   * @Title: checkBuyerisExist 
	   * @Description: 判断用户是否存在
	   * @author: guoconglin
	   * @param request
	   * @return
	   * @date: 2016年11月15日 下午5:22:41 
	   * @return: BaseResult
	   */
	  @RequestMapping(value = {"/checkIsExist"}, method = {RequestMethod.GET})
	  public @ResponseBody boolean checkBuyerIsExist(HttpServletRequest request,String mobile,String saleStatus){
	      logger.info("SkuProductRestController begin");
	      boolean isExist = true;
	      try {
	          if(saleStatus.equals("0")){
	             isExist =  buyerManageService.isExistMobile(mobile);    
	          }
	          if(saleStatus.equals("1")){
	               isExist =  supplyManageService.isExistMobile(mobile);    
	          }
	         return isExist;
        } catch (Exception e) {
            logger.info("checkBuyerIsExist error:" + e.getMessage());
            logger.info("SkuProductRestController checkBuyerIsExist end.");
           return false;
        }
	  }
	  /**
	   * 
	   * @Title: getSkuProductInfoById 
	   * @Description: 通过id获取发布商品信息
	   * @author: guoconglin
	   * @param request
	   * @param id
	   * @return
	   * @date: 2016年12月8日 上午11:42:15 
	   * @return: BaseResult
	   */
	 @RequestMapping(value="getSkuProductInfoById",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody BaseResult getSkuProductInfoById(HttpServletRequest request,String id){
	     logger.info("SkuProductRestController  getSkuProductInfoById begin.");
	     try {
	         logger.info("getSkuProductInfoById begin.");
            BaseResult baseResult = skuProductService.getSkuProductById(id);
            logger.info("getSkuProductInfoById end.");
            logger.info("SkuProductRestController  getSkuProductInfoById end.");
            return baseResult;
        } catch (Exception e) {
            logger.error("SkuProductRestController insertSkuProduct error:" + e.getMessage());
            logger.info("SkuProductRestController insertSkuProduct end.");
            return BaseResult.exception(e.getMessage());
        }
	}
	 @RequestMapping(value="/checkApproveStatus", method={RequestMethod.GET,RequestMethod.POST})
	 public @ResponseBody BaseResult checkApproveStatus(String approveStatus,String id){
	     logger.info("SkuProductRestController checkApproveStatus begin.");
	     try {
            logger.info("checkApproveStatus begin.");
            BaseResult baseResult = skuProductService.checkApproveStatus(approveStatus, id);
            logger.info("checkApproveStatus end.");
            logger.info("SkuProductRestController  checkApproveStatus end.");
            return baseResult;
        } catch (Exception e) {
            logger.error("SkuProductRestController checkApproveStatus error:" + e.getMessage());
            logger.info("SkuProductRestController checkApproveStatus end.");
            return BaseResult.exception(e.getMessage());
        }
	 }

	@RequestMapping(value="/modifySkuProduct",method={RequestMethod.POST})
	public @ResponseBody BaseResult modifySkuProduct(SkuProductVo skuProductVo){
		BaseResult result = skuProductService.modifySkuProduct(skuProductVo);
		return BaseResult.success(result);
	}


	@RequestMapping(value="/skuProductLogList",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult skuProductLogList(HttpServletRequest request,PaginationInfo paginationInfo,SkuProductLogConditionVo condition){
		logger.info("SkuProductRestController  skuProductLogList begin.");
		try {
			logger.info("skuProductLogList begin.");
			String skuProductId = request.getParameter("skuProductId");
			BaseResult baseResult = null ;
			if("" !=skuProductId ){
				condition.setSkuProductId(skuProductId);
			}

			baseResult = skuProductService.selectBySkuProductLogForPage(condition, paginationInfo);
			logger.info("skuProductLogList end.");
			logger.info("SkuProductRestController skuProductLogList end.");
			logger.info("basesResult:"+JSON.toJSONString(baseResult));
			return baseResult;
		} catch (Exception e) {
			logger.error("SkuProductRestController skuProductLogList error:" + e.getMessage());
			logger.error("SkuProductRestController skuProductLogList end.");
			return BaseResult.exception(e.getMessage());
		}
	}
}
